/*
 * @Author: ShiJunJie
 * @Date: 2020-11-03 10:01:50
 * @LastEditors: ShiJunJie
 * @LastEditTime: 2021-09-08 10:45:10
 * @Descripttion:
 */
import axios from 'axios'
import qs from 'qs'
// import notification from 'ant-design-vue/es/notification'
import storage from 'good-storage'

// import { message } from 'ant-design-vue'

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: import.meta.env.VITE_BASE_URL,
  timeout: 6000 // 请求超时时间
})
console.log(import.meta.env.VITE_BASE_URL)
// 异常拦截处理器
const errorHandler = (error) => {
  console.log(error)
  if (error.response) {
    const data = error.response.data
    // 从 localstorage 获取 token
    const user = storage.get('USER_TOKEN')
    if (error.response.status === 403) {
      // notification.error({
      //   message: 'Forbidden',
      //   description: data.message
      // })
      console.error({
        message: 'Forbidden',
        description: data.message
      })
    }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      // notification.error({
      //   message: 'Unauthorized',
      //   description: 'Authorization verification failed'
      // })
      console.error({
        message: 'Unauthorized',
        description: 'Authorization verification failed'
      })
      if (user) {
        storage.clear()
        setTimeout(() => {
          window.location.reload()
        }, 1500)
      }
    }
  }
  message.error(error?.response?.data?.message || error?.message)
  return Promise.reject(error)
}

// request interceptor  请求拦截器
request.interceptors.request.use((config) => {
  if (config.url.indexOf('/user/v1') !== -1) {
    config.baseURL += ':8001'
  }
  if (config.url.indexOf('/cms/v1') !== -1) {
    config.baseURL += ':8002'
  }
  if (config.url.indexOf('/eos/v1') !== -1) {
    config.baseURL = 'http://192.168.1.191:8002'
  }

  if (config.method === 'get') {
    config.url += config.parameter ? `?body=${JSON.stringify(parameter)}` : ''
  }

  if (config.method === 'post' || config.method === 'put') {
    config.headers['Content-Type'] = 'application/json'
    // console.log(config, typeof config.data)
    config.data = config?.data?.parameter || null
  }

  if (config.method === 'patch') {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    config.transformRequest = [
      function (data) {
        data = qs.stringify(data)
        return data
      }
    ]
  }
  const user = storage.get('USER_TOKEN')
  // console.log(user)
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (user) {
    config.headers['Authorization'] = user.token || ''
  }
  // console.log(`请求拦截器`, config)
  return config
}, errorHandler)

// response interceptor  响应拦截器
request.interceptors.response.use((response) => {
  // console.log(`响应拦截器`, response)
  const _response = { ...response.data }
  if (_response.code !== 0) message.error(_response.msg)
  return Promise.resolve(_response)
}, errorHandler)

export default request
